// miniprogram/pages/footerprint/footerprint.js

const { printList:PRINT_LIST, printDelete:PRINT_DELETE} = require('../../utils/api.js');
const MY_REQUEST = require('../../utils/myRequest.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取足迹列表
    MY_REQUEST.myRequest({
      url: PRINT_LIST,
      success:(res)=>{
        console.log('success',res.data.data);
        //我的足迹列表
        let resData = res.data.data.data;
        //按日期分类
        let my_FooterPrint = [];
        //临时对象
        let tempObj;
        resData.forEach((item)=>{
          let scrollX_to = new Array(item.length)
          //载入数据 date-日期 ，fp-足迹数组  scrollX_to-横向滑动距离
          tempObj = { 
            date: item[0].add_time, 
            fp: item, 
            scrollX_to
          }

          my_FooterPrint.push(tempObj)
        })
        this.setData({
          my_FooterPrint
        })
      },
      fail:(err)=>{
        console.log('fail',err);
      }
    })

    //设置顶部标题
    wx.setNavigationBarTitle({
      title: '我的足迹',
    });
  },

  //滑动开始
  touchStart(event){
    let touch_startX = event.touches[0].clientX;
    this.setData({
      touch_startX
    })
  },
  //滑动结束
  touchEnd(event){
    let touch_startX = this.data.touch_startX;
    let touch_endX = event.changedTouches[0].clientX;
    //获取触发滑动 对象 所属列的index
    let listIndex = event.currentTarget.dataset.listIndex;
    //获取触发滑动 对象 所属的列中 该对象的index
    let currIndex = event.currentTarget.dataset.currIndex;
    let my_FooterPrint = this.data.my_FooterPrint;
    //如果 两点相等
    if (touch_startX == touch_endX){
      return;
    }

    //判读 从左开始滑动 还是 从右开始滑动
    let flag_LeftTo = (touch_endX - touch_startX >= 8) && (touch_endX > touch_startX);
    let flag_RightTo = (touch_startX - touch_endX >= 15) && (touch_startX > touch_endX);
    //最终结果
    let flag = flag_LeftTo ? 'flag_LeftTo' : 'flag_RightTo';

    switch(flag){
      case 'flag_LeftTo':
        my_FooterPrint[listIndex].scrollX_to[currIndex] = 0;
        this.setData({
          my_FooterPrint
        });
        break;
      case 'flag_RightTo':
        //50是删除box的width
        my_FooterPrint[listIndex].scrollX_to[currIndex] = 50;
        this.setData({
          my_FooterPrint
        })
        break;
      default:
    }
  },

  //删除指定元素
  delete(event){
    wx.showToast({
      icon: 'loading'
    })
    let my_FooterPrint = this.data.my_FooterPrint;
    //获取触发滑动 对象 所属列的index
    let listIndex = event.currentTarget.dataset.listIndex;
    //获取触发滑动 对象 所属的列中 该对象的index
    let currIndex = event.currentTarget.dataset.currIndex;
    //根据下标 删除指定元素
    MY_REQUEST.myRequest({
      url: PRINT_DELETE,
      data:{
        footprintId: my_FooterPrint[listIndex].fp[currIndex].id
      },
      method: 'POST',
      success:()=>{
        console.log('删除成功')
        my_FooterPrint[listIndex].fp.splice(currIndex, 1);
        this.setData({
          my_FooterPrint
        })
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1500,
        })
      }
    })
  },
  //跳到详情页面
  toDetail(event){
    let goodsId = event.currentTarget.dataset.goodsId;
    wx.navigateTo({
      url: `../prodetail/prodetail?id=${goodsId}`,
    })
  },

  //加载更多足迹
  loadMore(){
    
  }
})